package com.vcc.service.impl;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryChain;
import com.mybatisflex.core.update.UpdateChain;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.vcc.core.exception.NotFoundException;
import com.vcc.core.util.DateUtil;
import com.vcc.core.util.RegexUtil;
import com.vcc.data.entity.UserRate;
import com.vcc.data.mapper.UserRateMapper;
import com.vcc.service.boss.request.rate.CreateUserRateDto;
import com.vcc.service.boss.request.rate.UpdateUserRateDto;
import com.vcc.service.service.ClientParamService;
import com.vcc.service.service.UserRateService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;

import static com.vcc.data.entity.table.UserRateTableDef.USER_RATE;

@Service
public class UserRateServiceImpl extends ServiceImpl<UserRateMapper, UserRate> implements UserRateService {

    @Resource
    private ClientParamService clientParamService;


    @Override
    public UserRate getById(Long id) {
        return queryChain().eq(UserRate::getId, id).oneOpt().orElseThrow(() -> new NotFoundException("没有用户费率"));
    }


    @Override
    public UserRate getByUserId(Long userId) {
        return queryChain().eq(UserRate::getUserId, userId).oneOpt().orElseThrow(() -> new NotFoundException("当前用户没有配置费率"));
    }

    @Override
    public BigDecimal getAuthRateByUserId(Long userId) {
        BigDecimal rate = mapper.findAuthRateByUserId(userId);
        return rate == null ? BigDecimal.valueOf(0) : rate.divide(BigDecimal.valueOf(100), 4, RoundingMode.HALF_UP);
    }

    @Override
    public BigDecimal getWithdrawRateById(Long userId) {
        BigDecimal withdrawRate = mapper.findWithdrawRateByUserId(userId);
        return withdrawRate == null ? BigDecimal.valueOf(0) : withdrawRate.divide(BigDecimal.valueOf(100), 4, RoundingMode.HALF_UP);
    }

    @Override
    public Long countByRateId(Long rateId) {
        return 0L;
    }


    @Override
    public Page<UserRate> paging(Page<UserRate> pageable, Long userId, Long rateId) {
        QueryChain<UserRate> queryChain = queryChain();
        if (RegexUtil.checkObjectIsNotNull(userId)) {
            queryChain.eq(UserRate::getUserId, userId);
        }
        return queryChain.page(pageable);
    }

    @Override
    public UserRate createUserRate(CreateUserRateDto createUserRateDto) {
        UserRate userRate;
        synchronized (this) {
            //如果当前用户没有此数值则创建
            userRate = queryChain().eq(UserRate::getUserId, createUserRateDto.getUserId()).oneOpt().orElse(new UserRate());
            userRate.setUserId(createUserRateDto.getUserId());
            userRate.setRate(createUserRateDto.getRate());
            userRate.setClearRate(createUserRateDto.getClearRate());
            //以下参数均已是否存在活动ID为准
            userRate.setActivityRate(RegexUtil.checkObjectIsNotNull(createUserRateDto.getActivityRate()) ? createUserRateDto.getActivityRate() : null);
            userRate.setActivityStartDate(RegexUtil.checkObjectIsNotNull(createUserRateDto.getActivityRate()) ? DateUtil.parse(createUserRateDto.getActivityStartDate()) : null);
            userRate.setActivityEndDate(RegexUtil.checkObjectIsNotNull(createUserRateDto.getActivityRate()) ? DateUtil.parse(createUserRateDto.getActivityEndDate()) : null);
            saveOrUpdate(userRate);
        }
        return userRate;
    }

    @Override
    public Boolean batchUpdateUserRate(UpdateUserRateDto updateUserRateDto) {
        UpdateChain<UserRate> updateChain = updateChain()
                .set(USER_RATE.RATE, updateUserRateDto.getRate())
                .set(USER_RATE.CLEAR_RATE, updateUserRateDto.getClearRate())
                .where(USER_RATE.USER_ID.in(updateUserRateDto.getUserId()));
        //以下参数均已是否存在活动ID为准
        if (RegexUtil.checkObjectIsNotNull(updateUserRateDto.getActivityRate())) {
            updateChain.set(USER_RATE.ACTIVITY_RATE, updateUserRateDto.getActivityRate())
                    .set(USER_RATE.ACTIVITY_START_DATE, DateUtil.parse(updateUserRateDto.getActivityStartDate()))
                    .set(USER_RATE.ACTIVITY_END_DATE, DateUtil.parse(updateUserRateDto.getActivityEndDate()));
        }
        return updateChain.update();
    }

    @Override
    public Boolean userInit(Long userId) {
        UserRate userRate = queryChain().eq(UserRate::getUserId, userId).oneOpt().orElse(null);
        if (userRate == null) {
            //如果当前用户没有此数值则创建
            String rechargeRate = clientParamService.getValueByCodeAndUserId("INIT_USER_RECHARGE_RATE", userId);
            String clearRate = clientParamService.getValueByCodeAndUserId("INIT_USER_WITHDRAW_RATE", userId);
            userRate = new UserRate();
            userRate.setUserId(userId);
            userRate.setRate(new BigDecimal(rechargeRate));
            userRate.setClearRate(new BigDecimal(clearRate));
            save(userRate);
        }
        return true;
    }
}
